package cn.it.shop.lucene;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.springframework.stereotype.Component;

import cn.it.shop.pojo.Category;
import cn.it.shop.pojo.Goods;

/**
 *  此类用来转化goods到document，或者document到goods
 */
@Component("documentUtil")
public class DocumentUtil {

	public Document goodsToDocument(Goods goods){
		Document doc=new Document();
		doc.add(new Field("id",goods.getId().toString(),Store.YES,Index.NOT_ANALYZED));
		doc.add(new Field("name",goods.getName(),Store.YES,Index.ANALYZED));
		doc.add(new Field("price",goods.getPrice().toString(),Store.YES,Index.NO));
		doc.add(new Field("remark",goods.getRemark(),Store.YES,Index.ANALYZED));
		doc.add(new Field("type",goods.getCategory().getType(),Store.YES,Index.NOT_ANALYZED));
		return doc;
	}
	
	public Goods documentToGoods(Document doc){
		Goods goods=new Goods();
		goods.setId(Integer.parseInt(doc.get("id")));
		goods.setName(doc.get("name"));
		goods.setPrice(Double.parseDouble(doc.get("price")));
		goods.setRemark(doc.get("remark"));
		Category category=new Category();
		category.setType(doc.get("type"));
		goods.setCategory(category);
		return goods;
	}
}
